﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FileHelper;

public partial class Folders_AddFolder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int parentid = -1;
            int folderid = -1;
            string owner = string.Empty;

            if (Request["parentid"] != null)
            {
                parentid = Convert.ToInt32(Request["parentid"]);
            }

            if (Request["folderid"] != null)
            {
                folderid = Convert.ToInt32(Request["folderid"]);
            }

            if (Request["owner"] != null)
            {
                owner = Request["owner"];
            }

            if (parentid > 0)
            {
                FolderInfo objFolder = FileStreamManager.GetFolder(parentid);
                this.lblParentFolderPath.Text = "Root\\" + objFolder.Path + "\\";
            }
            else
            {
                this.lblParentFolderPath.Text = "Root\\";
            }

            hdParentFolderId.Value = parentid.ToString();
            hdFolderId.Value = folderid.ToString();
            txtOwner.Text = owner;
            if (folderid <= 0)
            {
                this.lbtnSave.CommandName = "CMDADD";
            }
            else
            {
                this.lbtnSave.CommandName = "CMDEDIT";
            }
            
        }
    }
    protected void lbtnSave_Click(object sender, EventArgs e)
    {
        DoSaving();
    }
    private void Reset()
    {
        this.txtFolderName.Text = "";
        this.txtOwner.Text = "";        
        this.chkIsPublic.Checked = false;
    }
    private void DoSaving()
    {
        FolderInfo objFolder = new FolderInfo();
        if (this.lbtnSave.CommandName.ToUpper() == "CMDEDIT")
        {
            objFolder.Id = Convert.ToInt32(hdFolderId.Value);
        }
        else
        {
            objFolder.Id = -1;
        }
        objFolder.FolderName = txtFolderName.Text;
        objFolder.Owner = txtOwner.Text;
        objFolder.ParentId = Convert.ToInt32(hdParentFolderId.Value);
        objFolder.IsPublic = chkIsPublic.Checked;
        int folderId = FileStreamManager.CreateFolder(objFolder);
        if (folderId > 0)
        {
            ltrMessage.Text = "<span color='red'>Tạo folder thành công</span>";
            ltrMessage.Visible = true;
        }
    }

}